package com.jiaoyou.baojian.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.activity.RechargeActivity;
import com.jiaoyou.baojian.activity.TalkingActivity;
import com.jiaoyou.baojian.adapter.GiftAdapter;
import com.jiaoyou.baojian.adapter.SendDialogGiftAdapter;
import com.jiaoyou.baojian.entity.GiftEntity;
import com.jiaoyou.baojian.entity.GiveGiftListItem;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback2;
import com.jiaoyou.baojian.http.response.BaseResponse;

import java.util.List;

import retrofit2.Call;

/**
 * Created by DemonZYM on 17/5/6.
 */

public class SendGiftsDialog {
    private Dialog sendGiftsDialog;
    private Context context;

    public SendGiftsDialog(final Context context, long userid, final int yue, final OnSendListener listener){
        this.context = context;
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_send_gifts, null);
        final RecyclerView gifts = (RecyclerView) view.findViewById(R.id.gifts);
        final TextView sum = (TextView) view.findViewById(R.id.sum);
        Button send = (Button) view.findViewById(R.id.send);
        TextView yueView = (TextView) view.findViewById(R.id.yue);
        TextView recharge = (TextView) view.findViewById(R.id.recharge);

        yueView.setText("余额：" + yue + "豆");
        recharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RechargeActivity.show(context);
            }
        });

        final int[] zongjia = new int[1];
        final SendDialogGiftAdapter giftAdapter = new SendDialogGiftAdapter();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        gifts.setLayoutManager(gridLayoutManager);
        gifts.setAdapter(giftAdapter);
        giftAdapter.setEmptyView(getEmptyView());
        giftAdapter.notifyDataSetChanged();
        Api.getInstance().getGiftList(new BaofangCallback2<GiftEntity>(){
            @Override
            public void onFailure(Call<BaseResponse<GiftEntity>> call, Throwable t) {
                close();
                Toast.makeText(context, "网络错误：" + t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onSuccess(List<GiftEntity> giftEntities) {
                giftAdapter.setNewData(giftEntities);
                giftAdapter.notifyDataSetChanged();
                giftAdapter.setOnGiftPriceChangeListener(new SendDialogGiftAdapter.OnGiftPriceChangeListener() {
                    @Override
                    public void OnGiftPriceChange(int price) {
                        zongjia[0] = Integer.parseInt(sum.getText().toString()) + price;
                        sum.setText(zongjia[0] + "");
                        if(zongjia[0] > yue) {
                            sum.setTextColor(context.getResources().getColor(android.R.color.holo_red_light));
                        } else {
                            sum.setTextColor(context.getResources().getColor(R.color.main_black_gray));
                        }
                    }
                });
            }
        });

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(zongjia[0] > yue){
                    Toast.makeText(context, "余额不足", Toast.LENGTH_LONG).show();
                    return;
                }
                close();
                if(zongjia[0] != 0 && listener != null)
                    listener.onSendGifts(giftAdapter.getData());
            }
        });

        // 创建自定义样式的Dialog
        sendGiftsDialog = new Dialog(context, R.style.loading_dialog);
        sendGiftsDialog.setCancelable(true);
        sendGiftsDialog.setContentView(view, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
    }

    private View getEmptyView() {
        TextView textView = new TextView(context);
        textView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        textView.setGravity(Gravity.CENTER);
        textView.setText("加载中");
        return textView;
    }

    public void show(){
        sendGiftsDialog.show();
    }

    public void close(){
        if (sendGiftsDialog != null) {
            sendGiftsDialog.dismiss();
        }
    }

    public interface OnSendListener{
        void onSendGifts(List<GiftEntity> gifts);
    }
}
